class Solution {
public:
    bool check(vector<int>& nums, int k, int x)
    {
        int cnt = 1;
        int sum = 0;
        for (auto k : nums) {
            if (k > x)return 0;
            if (sum + k <= x)sum += k;
            else {
                sum = k;
                cnt++;
            }
        }
        return cnt <= k;
    }
    int splitArray(vector<int>& nums, int k) {
        int l = -1, r = 1e9 + 1;
        while (l + 1 < r) {
            int mid = (l + r) >> 1;
            if (check(nums, k, mid))r = mid;
            else l = mid;
        }
        return r;
    }
};

